package game.util
{
	import com.base.view.extend.NumberClip;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.ApplicationDomain;
	import flash.utils.*;
	
	import morn.core.components.Button;
	import morn.core.components.Clip;
	import morn.core.components.Component;
	import morn.core.components.Image;

	public class Util
	{
		public function Util()
		{
		}
		
		public static function getAvatarUrlById(uid:Number):String
		{
			var name:int = uid%10;
			return "icon/avatar/"+name+".jpg"
		}
		
		public static function parsePositionList(bd:BitmapData):Vector.<Point>
		{
			var vec:Vector.<Point> = new Vector.<Point>();
			var pixel:uint;
			var value:Boolean = false;
			var w:int = 0;
			var h:int = 0;
			while(h < bd.height)
			{
				value = false;
				w=0;
				while(w < bd.width)
				{
					pixel = bd.getPixel32(w,h);
					pixel = pixel >> 24 & 255;
					if(pixel==255)
					{
						if (value) vec.push(new Point(w,h));
						value=true;
					}
					else if (value) 
					{
						vec.pop();
						value=false;
						break;
					}
					if (value)w+=(5+Math.random()*10);
					else w++;
				}
				h+=(5+Math.random()*10);
			}
//			for each (var i:Point in vec) 
//			{
//				bd.setPixel32(i.x, i.y, 0x000000);
//			}
			return vec;
		}
		
		public static function numberToLengthText(value:Number,length:int):String
		{
			var temp:String = value.toString();
			while(temp.length<length)
			{
				temp = "0".concat(temp);
			}
			return temp;
		}
		
		
		
		
		
		
		
		private static function replaceSkinUrl(current:String,url:String):String
		{
			var temp:Array = current.split(".");
			if (temp&&temp.length>=3) 
			{
				temp[1] = url;
			}
			var then:String = temp.join(".");
			if (ApplicationDomain.currentDomain.hasDefinition(then)) return then;
			return current;
		}
		
		
		
		/**
		 * 截取字符串
		 * len 截取长度
		 */
		public static function interceptCharacter(str:String,len:int):String{
			if(str==null)str="";
			//trace("CarUtil interceptCharacter",str);
			var i:int=0;
			var _end:int=0;
			var _len:int=0;
			for (i = 0; i < str.length; i++)
			{
				
				if(str.charCodeAt(i)>10000){
					_len+=2;
				}else{
					_len+=1;
				}
				if(_len<=len){
					_end++;
				}
				//trace(str.charAt(i),str.charCodeAt(i),_end);
			}
			if(_end<1)_end=1;
			var _str:String="";
			for (i = 0; i < _end; i++)
			{
				_str+=str.charAt(i);
			}
			if(_str.length<str.length)_str=_str+"...";
			return _str;
		}
		/**
		 * 数字变化
		 * 
		 */
		public static function textNumChang(start_n:Number,end_n:Number):String{
			
			if(start_n==end_n)return end_n.toString();
			var _cstr:String="";
			var _nn:Number=end_n-start_n;
			_cstr=_nn.toString(); 
			var _nstr:String="";
			for(var i:int=0;i<_cstr.length;i++){
				if(i==0){
					_nstr+=(Math.floor(int(_cstr.charAt(i))*Math.random())).toString();
				}else{
					_nstr+=Math.floor(10*Math.random()).toString();
				}
				
			}
			if(_nn>0){
				_nstr=(start_n+Number(_nstr)).toString();
			}else{
				_nstr=(start_n-Number(_nstr)).toString();
			}
			return _nstr;
		}
		/**
		 * 添加滤镜
		 * @param child
		 * @param matrix 如果为null,添加灰色
		 * @param b:true添加，false移除
		 */
		public static function setFilter(child:DisplayObject,matrix:Array=null,b:Boolean=false):void{
			if(b){
				if(matrix==null){
					matrix = new Array();
					matrix = matrix.concat([.3,.59,.11,0,0]);// red
					matrix = matrix.concat([.3,.59,.11,0,0]);// green
					matrix = matrix.concat([.3,.59,.11,0,0]);// blue
					matrix = matrix.concat([0,0,0,1,0]);// alpha 		
				}
				var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
				child.filters = [filter];
			}else{
				child.filters = []; 
			}
		}
		
		
		
		
	}
}